<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
            color: #333;
        }

        h1, h2 {
            margin-bottom: 10px;
            padding-left: 20px;
        }

        h1 {
            font-size: 24px;
        }

        h2 {
            font-size: 20px;
        }

        form {
            margin-bottom: 20px;
            padding: 0 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        textarea {
            width: calc(100% - 40px);
            padding: 10px;
            font-size: 16px;
        }

        input[type="submit"], button {
            padding: 10px 15px;
            font-size: 16px;
            cursor: pointer;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
        }

        input[type="submit"]:hover, button:hover {
            background-color: #0056b3;
        }

        ul {
            list-style-type: none;
            padding: 0;
            padding-left: 20px;
        }

        li {
            margin-bottom: 10px;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        li:last-child {
            margin-bottom: 0;
        }

        section {
            margin: 20px;
        }

        /* 样式按钮 */
        .export-button {
            font-size: 12px; /* 更改按钮大小 */
        }

        .delete-button {
            color: red; /* 将删除按钮颜色更改为红色 */
            font-size: 12px; /* 将删除按钮大小更改为与导出按钮相同 */
        }

        pre {
            background-color: #f3f0f0;
        }
    </style>
</head>

<body>
<header>
    <h1>构建数据集</h1>
</header>

<section>
    <h2>批量创建数据集</h2>
    <form id="createDataSet">
        <textarea id="formData" name="formData" rows="15" cols="50" placeholder="question:法国的首都是什么？,
groundTruths:巴黎,
answer:巴黎,
contexts:巴黎是法国的首都。,

question:《哈利波特》的作者是谁？,
groundTruths:J.K.罗琳,
answer:J.K.罗琳,
contexts:J.K.罗琳写了《哈利波特》。,

question:水的沸点是多少？,
groundTruths:100度摄氏度,
answer:100度摄氏度,
contexts:水在海平面下沸腾的温度是100摄氏度。,
"></textarea>
        <input type="submit" value="提交">
    </form>
</section>

<section>
    <h2>表单列表</h2>
    <ul id="formsList"></ul>
</section>

<script>

    class ajax {
        static baseURL = '';

        /**
         * 发起请求
         * @param url
         * @param options
         * @returns {Promise<any>}
         */
        static async request(url, options) {
            let response = await fetch(`${ajax.baseURL}${url}`, options);
            return response.json();
        }

        /**
         *
         * @param url
         * @returns {Promise<*>}
         */
        static async get(url) {
            return ajax.request(url, {});
        }

        /**
         *
         * @param url
         * @param data
         * @returns {Promise<*>}
         */
        static async post(url, data) {
            let options = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data),
            };
            return ajax.request(url, options);
        }

        /**
         *
         * @param url
         * @returns {Promise<*>}
         */
        static async delete(url) {
            let options = {
                method: 'DELETE',
            };
            return ajax.request(url, options);
        }
    }
</script>
<script>

    ajax.baseURL = 'http://localhost:8000'


    /**
     * 显示表单列表
     * @returns {Promise<void>}
     */
    async function displayForms() {
        let formsList = document.getElementById('formsList');
        formsList.innerHTML = '';
        let result = await ajax.get('/dataset/');
        result.forEach(r => {
            let listItem = `
            <li>
                <label>${r.pid}</label>
                <button class="export-button" onclick="exportDataset(${r.pid})">导出</button>
                <button class="delete-button" onclick="deleteDataset(${r.pid})">X</button>
                <pre>${JSON.stringify(r.data, null, 4)}</pre>
            </li>
            `;

            formsList.insertAdjacentHTML('beforeend', listItem)
        });
    }

    /**
     * 创建数据集
     */
    document.getElementById('createDataSet').addEventListener('submit', async (event) => {
        event.preventDefault();
        let formData = document.getElementById('formData').value.trim();
        if (!formData) return
        // 向服务端发起请求
        await ajax.post('/dataset/ragas', {data: formData});
        // // 向服务端发起请求
        // await ajax.post('/dataset/chatglm', {data: formData});
        // 刷新表单列表
        await displayForms();
    });


    /**
     * 导出数据集
     * @param pid
     * @returns {Promise<void>}
     */
    async function exportDataset(pid) {

        try {
            // 通过 AJAX 请求获取数据压缩包
            let response = await fetch(`${ajax.baseURL}/export-dataset/${pid}`);

            // 检查响应状态码
            if (!response.ok) {
                throw new Error('导出数据失败');
            }

            // 读取响应的数据流
            let blob = await response.blob();

            // 创建一个链接元素以下载文件
            let url = URL.createObjectURL(blob);
            let a = document.createElement('a');
            a.href = url;
            // 设置下载的文件名为 data.zip
            a.download = `data_${pid}.zip`;
            // 模拟点击链接以触发下载
            a.click();
        } catch (error) {
            // 如果出现错误，则显示导出数据失败的警告
            alert(error.message);
        }
    }


    /**
     * 删除数据集
     * @param pid
     * @returns {Promise<void>}
     */
    async function deleteDataset(pid) {
        try {
            await fetch(`${ajax.baseURL}/dataset/${pid}/`, {method: 'DELETE'});
            await ajax.delete(`/dataset/${pid}/`);
            await displayForms(); // 删除成功后刷新表单列表
        } catch (error) {
            alert('删除数据集失败：' + error.message);
        }
    }

    // 初始化页面
    displayForms();
</script>
</body>
</html>
